package milk.ui
{
	import com.crlui.control.scrolls.Direction;
	import com.crlui.event.ResizeEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class ExTextArea extends Sprite
	{
		private var tf:TextField;
		private var scrollBar:Slider;
		
		private var barW:int=16;
		public function ExTextArea()
		{
			super();
			
			tf=new TextField();
			//tf.autoSize=TextFieldAutoSize.LEFT;
			var format:TextFormat=new TextFormat();
			format.leading=5;
			tf.defaultTextFormat=format;
			tf.wordWrap=true;
			tf.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelHandle);
			scrollBar=new Slider(_height,barW,Direction.VERTICAL);
			scrollBar.onChanged=onChanged;
			
			
			this.addChild(tf);
			this.addChild(scrollBar);
			
			this.addEventListener(ResizeEvent.RESIZE,resizeHandle);
		}
		
		public function set textFormat(value:TextFormat):void{
			if(tf.text){
				tf.setTextFormat(value);
			}
			tf.defaultTextFormat=value;
		}
		
		private function mouseWheelHandle(event:Event):void{
			var value:Number=(tf.scrollV-1)/(tf.maxScrollV-1);
			
			scrollBar.setValue(value);
		}
		private function resizeHandle(event:Event):void{
			
			tf.width=_width-barW;
			tf.height=height;
			
			scrollBar.size=height;
			scrollBar.x=tf.width;
			
			scrollBar.setKnobSize(height/tf.textHeight);
		}
		
		
		private function onChanged(bar:Slider):void{
			var value:Number=bar.getValue();
			tf.scrollV=Math.ceil(tf.maxScrollV*value);
		}
		
		
		public function set html(value:String):void{
			tf.htmlText=value;
			scrollBar.setKnobSize(height/tf.textHeight);
		}
		
		protected var _width:int=100;
		protected var _height:int=100;
		
		override public function set height(value:Number):void
		{
			_height = value;
			
			this.dispatchEvent(new ResizeEvent(ResizeEvent.RESIZE));
		}
		
		override public function get height():Number{
			return _height;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			this.dispatchEvent(new ResizeEvent(ResizeEvent.RESIZE));
		}
		
		override public function get width():Number{
			return _width;
		}
		
	}
}